package xyz.xtt.asset.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.json.JSONUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.asset.client.response.TblAssetExpandDto;
import xyz.xtt.asset.request.OperateDownRequest;
import xyz.xtt.asset.request.OperateUpRequest;
import xyz.xtt.asset.service.ITblAssetService;
import xyz.xtt.asset.service.ITblAssetStaticResourceService;
import xyz.xtt.asset.service.ITblAssetUserLogService;
import xyz.xtt.asset.vo.MyAssetsResponse;
import xyz.xtt.common.response.ApiResult;

@Tag(name = "资产管理", description = "订单管理")
@RefreshScope
@Slf4j
@RequestMapping("/api/nft/asset")
@RestController
public class AssetController {
	@Autowired
	private ITblAssetService iTblAssetService;
	@Autowired
	private ITblAssetUserLogService iTblAssetUserLogService;
	@Autowired
	private ITblAssetStaticResourceService iTblAssetStaticResourceService;

	@Operation(summary = "我的资产")
	@PostMapping("/myAssets")
	public ApiResult<MyAssetsResponse> myAssets(@RequestHeader("uid") String uid, Integer currPage, Integer type) {
		log.info("/api/asset/nft/myAssets uid:{},currPage:{},type:{}", uid, currPage, type);
		MyAssetsResponse response = iTblAssetService.getMyAssets(uid, currPage, type);
		return ApiResult.ok(response);
	}

	@Operation(summary = "获取资产最新配置及其日志")
	@GetMapping("/getAssetExpand")
	public ApiResult<TblAssetExpandDto> getAssetExpand(@RequestParam("assetId") @NotNull Long assetId) {
		log.info("/api/asset/nft/getAssetExpand assetId:{}", assetId);
		return ApiResult.ok(iTblAssetService.getAssetExpand(assetId));
	}

	@Operation(summary = "上架")
	@PostMapping("/on")
	public ApiResult<Boolean> on(@RequestHeader("uid") String uid, @RequestBody @Valid OperateUpRequest operateUpRequest) {
		log.info("/api/exchange/operate/on uid:{} param: {}", uid, JSONUtil.toJsonStr(operateUpRequest));
		Boolean order = iTblAssetUserLogService.putOn(uid, operateUpRequest);
		return ApiResult.ok(order);
	}

	@Operation(summary = "下架")
	@PostMapping("/off")
	public ApiResult<Boolean> off(@RequestHeader("uid") String uid, @RequestBody @Valid OperateDownRequest orderRequest) {
		log.info("/api/exchange/operate/off uid:{} param: {}", uid, JSONUtil.toJsonStr(orderRequest));
		Boolean order = iTblAssetUserLogService.putOff(uid, orderRequest);
		return ApiResult.ok(order);
	}

	@GetMapping("/set_certificate_uri")
	public ApiResult<Boolean> setCertificateUri(@RequestParam("assetId") Long assetId, @RequestParam("certificateUri") String certificateUri) {
		return ApiResult.ok(iTblAssetStaticResourceService.setCertificateUri(assetId, certificateUri));
	}
}
